package org.bdware.irs.backend.dto;

import org.bdware.irs.backend.utils.DOIParser;
import org.bdware.irp.stateinfo.UserStateInfo;

public class User {

    private Long id;
    private String doi;
    private String suffix;
    private String organization;
    private String publicKey;
    private String lastModified;
    private String des;


    public UserStateInfo toUserStateInfo() {
        UserStateInfo userStateInfo = new UserStateInfo(this.getPublicKey(),this.getDes());
        userStateInfo.setIdentifier(doi);
        userStateInfo.getHandleValues().addProperty("organization", organization);
        userStateInfo.getHandleValues().addProperty("suffix", suffix);
        userStateInfo.getHandleValues().addProperty("lastModified", lastModified);
        return userStateInfo;
    }

    public User(UserStateInfo userStateInfo){
        if(userStateInfo == null)
            return;
        this.doi = userStateInfo.identifier;
        this.suffix = DOIParser.getSuffix(this.doi);
        this.publicKey = userStateInfo.getPubkey();
        this.des = userStateInfo.getDesc();
        if(userStateInfo.getHandleValues().get("organization") != null)
            this.organization = userStateInfo.getHandleValues().get("organization").getAsString();
        if(userStateInfo.getHandleValues().get("lastModified") != null)
            this.lastModified = userStateInfo.getHandleValues().get("lastModified").getAsString();
    }

    public User() {
    }

    public User(Long id, String doi, String suffix, String organization, String publicKey) {
        this.id = id;
        this.suffix = suffix;
        this.organization = organization;
        this.publicKey = publicKey;
    }

    public User(Long id, String doi, String suffix, String organization, String publicKey, String lastModified, String des) {
        this.id = id;
        this.doi = doi;
        this.suffix = suffix;
        this.organization = organization;
        this.publicKey = publicKey;
        this.lastModified = lastModified;
        this.des = des;
    }

    public String getLastModified() {
        return lastModified;
    }

    public void setLastModified(String lastModified) {
        this.lastModified = lastModified;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getDoi() {
        return doi;
    }

    public void setDoi(String doi) {
        this.doi = doi;
    }

    public void update(User user){
        setDes(user.des);
        setOrganization(user.organization);
        setPublicKey(user.publicKey);
        setLastModified(System.currentTimeMillis()+"");
    }


}